# -*- coding: utf-8 -*-
"""
__title__ = ''
__author__ = 'Yang'
__mtime__ = '2018/10/27'
# code is far away from bugs with the god animal protecting
    I love animals. They taste delicious.
              ┏┓      ┏┓
            ┏┛┻━━━┛┻┓
            ┃      ☃      ┃
            ┃  ┳┛  ┗┳  ┃
            ┃      ┻      ┃
            ┗━┓      ┏━┛
                ┃      ┗━━━┓
                ┃  神兽保佑    ┣┓
                ┃　永无BUG！   ┏┛
                ┗┓┓┏━┳┓┏┛
                  ┃┫┫  ┃┫┫
                  ┗┻┛  ┗┻┛
"""
import re
import requests
import time


def open_url(url):
    r = requests.get(url)
    r.encoding = r.apparent_encoding
    return r

def find_page_list(l):
    # http://t2.hddhhn.com/uploads/tu/201810/9999/fb537690f4.jpg
    reg = re.compile('<img.alt=".*?".*?src="(.*?.jpg)"./>')
    reg1 = re.compile("<a.href='([0-9].*?\.html)'>\d{1}?</a>")
    page_lsit = []
    for i in l:
        print(i)
        time.sleep(10)
        r = open_url(i)
        page_lsit = reg1.findall(r.text)
        img_utf = reg.findall(r.text)
        print('img_utf', img_utf)
        print(page_lsit)
        try:
            with open('G:/img1/' + str("%d" % time.time()) + '.jpg', 'wb') as f:
                f.write(requests.get(img_utf[0]).content)
        except:
            print(page_lsit, img_utf)

        for i in page_lsit:
            time.sleep(1)
            u = page_lsit[:41]
            u.append(i)
            print(u)
            r = open_url(u)
            img_utf = reg.findall(r.text)
            with open('G:/img1/' + str("%d" % time.time()) + '.jpg', 'wb') as f:
                f.write(requests.get(img_utf[0]).content)
            print(img_utf)


def main(reg):
    for i in range(1, 11):
        url = 'http://www.27270.com/ent/meinvtupian/list_11_' + str(i) + '.html'
        r = open_url(url)
        time.sleep(1)
        l = reg.findall(r.text)
        print(l)
        find_page_list(l)


if __name__ == '__main__':
    reg = re.compile(' <a.href="(.*?.html)".title=".*?".class=".*?".target="_blank">')
    main(reg)
